﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service {
    public interface IOrderModelCache {
        T GetOrderModelCache<T>(string key);
        void SetOrderModelCache<T>(T model, string key);
        void ClearOrderModelCache(string key);
    }

    public class OrderModelCacheService : IOrderModelCache {
        private const string order_fx = "token_mall_order_{0}";
        private IUnitOfWork _iUnitOfWork;
        public OrderModelCacheService(IUnitOfWork iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
        }

        public void ClearOrderModelCache(string key) {
            key = string.Format(order_fx, key);
            _iUnitOfWork.RomoveCache(key);
        }

        public T GetOrderModelCache<T>(string key) {
            key = string.Format(order_fx, key);
            var result = _iUnitOfWork.GetCache<T>(key);
            return result;
        }

        public void SetOrderModelCache<T>(T model, string key) {
            key = string.Format(order_fx, key);
            _iUnitOfWork.SetCache(key, model, 600);
        }
    }
}
